from sparkai.llm.llm import ChatSparkLLM, ChunkPrintHandler
from sparkai.core.messages import ChatMessage
import E_command
import time

def inf():
    model_name = "Elysium_XF"
    model_function = "Elysium Spark3.5 Max模型调用模块,本AI技术支持来自于讯飞星火,详细调试参数见设置后台"
    return model_name, model_function

SPARKAI_URL = 'wss://spark-api.xf-yun.com/v3.5/chat'
SPARKAI_APP_ID = '2a422c2e'
SPARKAI_API_SECRET = 'ZWZkOTc2YzU4OTAxOGY4MDJhYjk5MmM0'
SPARKAI_API_KEY = '4e6071486b3da471e5f56753534c1d67'
SPARKAI_DOMAIN = 'generalv3.5'

def print_with_animation(text):
    for char in text:
        print(char, end='', flush=True)
        time.sleep(0.05)  # 控制输出速度
    print()  # 换行

def Elysium_XF():
    E_command.command_elysium("欢迎使用Elysium_讯飞星火Spark3.5 Max！请输入你的问题或者对话内容，输入'exit'来退出对话。")
    spark = ChatSparkLLM(
        spark_api_url=SPARKAI_URL,
        spark_app_id=SPARKAI_APP_ID,
        spark_api_key=SPARKAI_API_KEY,
        spark_api_secret=SPARKAI_API_SECRET,
        spark_llm_domain=SPARKAI_DOMAIN,
        streaming=False,
    )
    messages = []
    while True:
        text = input("\nQ: ")
        if text.lower() == "exit":
            E_command.command_elysium("Elysium: 再见！欢迎下次再来！")
            break
        user_message = ChatMessage(role="user", content=text)
        messages.append(user_message)
        handler = ChunkPrintHandler()
        result = spark.generate([messages], callbacks=[handler])
        if result and result.generations:
            response = str(result.generations[0])  # Convert to string
            start_index = response.find("text='") + len("text='")
            end_index = response.find("', message=AIMessage")
            response = response[start_index:end_index]
            print("A:", end=' ')
            print_with_animation(response.replace("\\n", "\n"))  # 替换 \n 为换行符
            ai_message = ChatMessage(role="assistant", content=response)
            messages.append(ai_message)
        else:
            E_command.command_warn("网络故障")
